﻿using System;
using System.Text.RegularExpressions;

namespace MDAControlsFramework.Helpers {
	/// <summary>
	/// slouží pro uchování rozměru na obrazovce
	/// umožňuje přepočet souřadnic podle rozměru obrazovky
	/// </summary>
	public class MDAValue: ICloneable {
		public SizeType _sizeType = SizeType.absolute;
		public int _originalValue = 100;
		public int _computedValue = 0;
		private bool _canCompute = true;

		public MDAValue() {
		}

		public MDAValue(string value) {
			SetValue(value);
		}

		public object Clone() {
			return this.MemberwiseClone();
		}

		public int ComputedValue {
			get {
				return _computedValue;
			}
		}

		public void SetValue(string value) {
			if (value == "*") {
				_canCompute = true;
				return;
			}

			if (!_canCompute) {
				return;
			}
			Regex nonNumber = new Regex(@"\D");
			_computedValue = Int32.Parse(nonNumber.Replace(value, ""));

			if (value.EndsWith("%")) {
				_sizeType = SizeType.percent;
			} else {
				if (value.EndsWith("pt")) {
					_sizeType = SizeType.relative;
				} else {
					_sizeType = SizeType.absolute;
				}
			}
		}

		public void SetValue(int value) {
			_canCompute = false;
			_computedValue = value;
		}

		public void Measure(int maxValue) {
			switch (_sizeType) {
				case SizeType.absolute:
					_computedValue = _originalValue;
					break;
				case SizeType.percent:
					_computedValue = _originalValue * maxValue / 100;
					break;
				case SizeType.relative:
					if (maxValue == 320 || maxValue == 640) {
						_computedValue = _originalValue;
					}
					if (maxValue == 640 || maxValue == 480) {
						_computedValue = _originalValue * 2;
					}
					break;
			}
		}

		public enum SizeType {
			absolute,
			relative,
			percent
		}

		public override string ToString() {
			return _computedValue.ToString();
		}
	}
}